package com.example.ggmusic;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.IOException;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    /* 底部导航组件 */
    
    /**
     * 底部导航视图
     */
    private BottomNavigationView bottomNavigation;
    /**
     * 歌曲名
     */
    private TextView tvBottomTitle;
    /**
     * 歌手名
     */
    private TextView tvBottomArtist;
    /**
     * 专辑封面图
     */
    private ImageView ivAlbumThumbnail;
    /**
     * 播放按钮图标
     */
    private ImageView ivPlay;
    /**
     * 播放状态
     */
    private Boolean ivPlayStatus = true;
    /**
     * 播放进度条
     */
    private ProgressBar pbProgress;

    /* 媒体播放器 MediaPlayer */
    /**
     * 媒体播放器
     */
    private final MediaPlayer mMediaPlayer = null;

    /* 内容解析器 ContentResolver */
    /**
     * 内容解析器
     */
    private ContentResolver mContentResolver;
    /**
     * 选择语句where子句
     */
    private final String SELECTION = MediaStore.Audio.Media.IS_MUSIC + " = ? " + " AND " + MediaStore.Audio.Media.MIME_TYPE + " LIKE ? ";
    /**
     * where子句参数
     */
    private final String[] SELECTION_ARGS = {Integer.toString(1), "audio/mpeg"};
    /**
     * 请求外部存储
     */
    private final int REQUEST_EXTERNAL_STORAGE = 1;
    /**
     * 权限存储
     */
    private static final String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_MEDIA_IMAGES,
            Manifest.permission.READ_MEDIA_VIDEO,
            Manifest.permission.READ_MEDIA_AUDIO
    };

    /* 媒体光标适配器 MediaCursorAdapter */
    /**
     * 列表显示
     */
    private ListView mPlaylist;
    /**
     * 媒体光标适配器
     */
    private MediaCursorAdapter mCursorAdapter;

    /* 后台服务 相关常量 */
    /**
     * 日期 URI
     */
    public static final String DATA_URI = "com.example.ggmusic.DATA_URI";
    /**
     * 歌名
     */
    public static final String TITLE = "com.example.ggmusic.TITLE";
    /**
     * 歌手
     */
    public static final String ARTIST = "com.example.ggmusic.ARTIST";

    /* 监听器 */
    /**
     * 歌曲界面事件监听器
     */
    private final ListView.OnItemClickListener mPlaylistClickListener = new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int i, long l) {

            Cursor cursor = mCursorAdapter.getCursor();
            if (cursor != null && cursor.moveToPosition(i)) { // cursor.moveToPosition(i) 移动到指定行
                /* 获取索引 */
                int titleIndex = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
                int artistIndex = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
                int albumIdIndex = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
                int dataIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);

                /* 获取资源 */
                String title = cursor.getString(titleIndex);
                String artist = cursor.getString(artistIndex);
                long albumId = cursor.getLong(albumIdIndex);
                String data = cursor.getString(dataIndex);

                /* 启动前台服务播放音乐 */
                Intent serviceIntent = new Intent(MainActivity.this, MusicService.class);
                serviceIntent.putExtra(MainActivity.DATA_URI, data);
                serviceIntent.putExtra(MainActivity.TITLE, title);
                serviceIntent.putExtra(MainActivity.ARTIST, artist);
                startService(serviceIntent);

                /* 设置底部导航栏开始 不可见 */
                bottomNavigation.setVisibility(View.VISIBLE);

                /* 将 歌名 绑定到控件上 */
                if (tvBottomTitle != null) tvBottomTitle.setText(title);
                /* 将 歌手 绑定到控件上 */
                if (tvBottomArtist != null) tvBottomArtist.setText(artist);
                /* 将 封面 绑定到控件上 */
                Uri albumUri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, albumId); // 查询
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    try {
                        Bitmap album = mContentResolver.loadThumbnail(albumUri, new Size(640, 480), null);
                        ivAlbumThumbnail.setImageBitmap(album);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };
    /**
     * 播放按钮单击 监听器
     */
    private final View.OnClickListener ivPlayButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.iv_play) {
                ivPlayStatus = !ivPlayStatus;
                if (ivPlayStatus) {
                    mService.play();
                    ivPlay.setImageResource(R.drawable.ic_baseline_pause_circle_outline_24);
                } else {
                    mService.pause();
                    ivPlay.setImageResource(R.drawable.ic_baseline_play_circle_outline_24);
                }
            }
        }
    };


    /* 绑定MusicService服务 */
    /**
     * 音乐服务类实例
     */
    private MusicService mService;
    /**
     * ServiceConnection接口对象
     */
    private final ServiceConnection mConn = new ServiceConnection() {
        /**
         * 对mService进行初始化
         * @param componentName 组件名称
         * @param iBinder MusicService的onBind()方法返回的IBinder对象
         */
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            MusicService.MusicServiceBinder binder = (MusicService.MusicServiceBinder) iBinder;
            mService = binder.getService();
        }

        /**
         * 对mService进行初始化
         * @param componentName 组件名称
         */
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mService = null;
        }
    };


    /* 更新音乐播放进度状态 */
    /**
     * 用于区分Message的类别
     */
    public static final int UPDATE_PROGRESS = 1;
    /**
     * 子线程与主线程间的数据传递
     */
    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            if (msg.what == UPDATE_PROGRESS) {
                int position = msg.arg1;
                pbProgress.setProgress(position);
            }
        }
    };
    /**
     * 实现Runnable接口，可在Thread线程类中进行运行.主要是简化项目功能实现工程
     */
    private class MusicProgressRunnable implements Runnable {
        public MusicProgressRunnable() {
        }

        @Override
        public void run() {
            boolean mThreadWorking = true;
            while (mThreadWorking) {
                try {
                    if (mService != null) {
                        int position = mService.getCurrentPosition();
                        Message message = new Message();
                        message.what = UPDATE_PROGRESS;
                        message.arg1 = position;
                        mHandler.sendMessage(message);
                    }
                    assert mService != null;
                    mThreadWorking = mService.isPlaying();
                    Thread.sleep(100);
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    }


    /* 自定义MusicReceiver广播 */
    /**
     * 音乐开始
     */
    public static final String ACTION_MUSIC_START = "com.example.ggmusic.ACTION_MUSIC_START";
    /**
     * 音乐停止
     */
    public static final String ACTION_MUSIC_STOP = "com.example.ggmusic.ACTION_MUSIC_STOP";
    /**
     * 内部类 MusicReceiver
     */
    public class MusicReceiver extends BroadcastReceiver {
        /**
         * 当音乐开始播放后，开启MusicProgressRunnable线程执行查询音乐播放状态
         * MusicService服务在MediaPlayer类完成prepare()、start()方法调用后发送该广播，
         * 从而实现MusicService所在线程与MusicProgressRunnable所在线程同步
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mService != null) {
                pbProgress.setMax(mService.getDuration());
                new Thread(new MusicProgressRunnable()).start();
            }
        }
    }
    /**
     * 内部类 MusicReceiver 实例
     */
    private MusicReceiver musicReceiver;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 实例化相关对象
        mContentResolver = getContentResolver();
        mCursorAdapter = new MediaCursorAdapter(MainActivity.this);

        // 绑定 ListView
        mPlaylist = findViewById(R.id.lv_playlist);

        // 设置适配器
        mPlaylist.setAdapter(mCursorAdapter);

        // 绑定 BottomNavigationView
        bottomNavigation = findViewById(R.id.navigation); // 拿到主布局的底部导航栏对象
        LayoutInflater.from(MainActivity.this).inflate(R.layout.bottom_media_toolbar, bottomNavigation, true); // 把 bottom_media_toolbar 放到主布局的底部导航栏中

        // 让底部导航栏一开始消失的
        bottomNavigation.setVisibility(View.GONE);

        // 绑定 BottomNavigationView 其他控件
        tvBottomTitle = bottomNavigation.findViewById(R.id.tv_bottom_title);
        tvBottomArtist = bottomNavigation.findViewById(R.id.tv_bottom_artist);
        ivAlbumThumbnail = bottomNavigation.findViewById(R.id.iv_thumbnail);
        pbProgress = bottomNavigation.findViewById(R.id.progress);
        ivPlay = bottomNavigation.findViewById(R.id.iv_play);

        // 查询权限，判断第一次打开APP时是否点了是
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE))
                requestPermissions(PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE); // 如果第一次点了是，下次不会弹出; 如果点了否，下次还会弹出
        } else initPlaylist();

        // 设置监听器
        if (ivPlay != null) ivPlay.setOnClickListener(ivPlayButtonClickListener);
        mPlaylist.setOnItemClickListener(mPlaylistClickListener);

        // 通过 registerReceiver 注册 MusicReceiver 广播
        musicReceiver = new MusicReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_MUSIC_START);
        intentFilter.addAction(ACTION_MUSIC_STOP);
        registerReceiver(musicReceiver, intentFilter);
    }

    /**
     * 权限管理
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_EXTERNAL_STORAGE) {// 判断授权结果
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                initPlaylist();
        }
    }

    /**
     * 播放、暂停按钮 点击事件触发器
     */
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_play) {
            ivPlayStatus = !ivPlayStatus;
            if (ivPlayStatus) {
                mService.play();
                ivPlay.setImageResource(R.drawable.ic_baseline_pause_circle_outline_24);
            } else {
                mService.pause();
                ivPlay.setImageResource(R.drawable.ic_baseline_play_circle_outline_24);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(MainActivity.this, MusicService.class);
        // Intent - 表示绑定服务的Intent对象，与startService()方法中的Intent对象相同
        // ServiceConnection - 表示服务连接接口，在绑定与解绑服务时，接口的相应方法被回调
        // int - 表示绑定服务的动作行为，在本例中使用BIND_AUTO_CREATE，表示自动创建服务
        bindService(intent, mConn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        unbindService(mConn);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(musicReceiver);
        super.onDestroy();
    }

    /**
     * 查询MP3数据
     */
    private void initPlaylist() {
        /* 游标对象查询MP3数据 */
        Cursor mCursor = mContentResolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, // 表名
                null,   // 所有列
                SELECTION,        // where子句
                SELECTION_ARGS,   // where参数
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER // 默认排序方式
        );
        mCursorAdapter.swapCursor(mCursor);
        mCursorAdapter.notifyDataSetChanged();
    }
}